package com.probiz.estoremf.product.web.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.catalog.service.ProductCategoryManager;
import com.probiz.estore.common.model.product.Product;
import com.probiz.estore.common.model.sales.RecommendedType;
import com.probiz.estore.core.controller.GenericStoreFrontController;
import com.probiz.estore.core.model.BaseObject;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.product.service.ProdMainManager;
import com.probiz.estore.sales.service.EvalRecommendationManager;
import com.probiz.estore.sales.service.RecommendedTypeManager;
import com.probiz.estore.webapp.util.RequestUtil;

public class RecommendedProductListFrontController extends	GenericStoreFrontController<RecommendedType> {

	private EvalRecommendationManager evalRecommendationManager = null;
	private RecommendedTypeManager recommendedTypeManager = null;
	private ProdMainManager prodMainManager = null;
	private ProductCategoryManager productCategoryManager = null;

	protected void initController() throws Exception {
		mgr = recommendedTypeManager;
	}

	public void setEvalRecommendationManager(
			EvalRecommendationManager evalRecommendationManager) {
		this.evalRecommendationManager = evalRecommendationManager;
	}

	public void setProdMainManager(ProdMainManager prodMainManager) {
		this.prodMainManager = prodMainManager;
	}
	
	public void setRecommendedTypeManager(RecommendedTypeManager recommendedTypeManager) {
		this.recommendedTypeManager = recommendedTypeManager;
	}
	

	public void setProductCategoryManager(ProductCategoryManager productCategoryManager) {
		this.productCategoryManager = productCategoryManager;
	}

	public ModelAndView defaultAction(HttpServletRequest request,
			HttpServletResponse response) {
		// -----start:获得推荐类型------
		Integer recommendedTypeId = recommendedTypeManager.getRecommendedTypeIdByUri(request.getRequestURI());
		RecommendedType recommendedType = recommendedTypeManager.getById(recommendedTypeId);
		// 没有找到相应推荐类型不能访问,直接进入找不到或已删除页面
		if (recommendedType == null) {
			try {
				response.sendError(404);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		// -----end:获得推荐类型------

		SearchCriteria searchCriteria = createSearchCriteria(request, BaseObject.FilterHqlName.DEFAULT);
		// ------start:改变页面显示条数 只有不是默认值的时候才需改变------
		if (searchCriteria.getPageSize() == defaultPageSize) {
			searchCriteria.changePaging(searchCriteria.getPageNo(), 20);
		}
		// ------end:改变页面显示条数 只有不是默认值的时候才需改变------
		
		// ------start:构造前台查询条件------
		Integer sourceKind = RequestUtil.getInteger(request, "sourceKind");
		String sourceIds = RequestUtil.getParameterNullSafe(request, "sourceIds");
		List<Integer> sourceIdList = new ArrayList<Integer>();
		if(StringUtils.isNotBlank(sourceIds)){
			for(String id:sourceIds.split(",")){
				sourceIdList.add(Integer.parseInt(id));
			}
		}	
		
		List<Product> productList = evalRecommendationManager.getProductsByRecommendedTypeNameBySourceIdList(recommendedType.getTypeName(),sourceKind,sourceIdList,0,20);
		// ------end:构造前台查询条件------

		ModelAndView mv = new ModelAndView("product/recommendedProductList");
		mv.addObject("productList", productList);
		mv.addObject("recommendedType", recommendedType);
		if(sourceIdList.size()>0){
			mv.addObject("category", productCategoryManager.getById(sourceIdList.get(0)));
		}
		mv.addObject("sourceKind", sourceKind);
		mv.addObject("sourceIds", sourceIds);
		// ------start:把页面list的显示风格设置到attribute中------
		Cookie[] cookies = request.getCookies();
		if (!ArrayUtils.isEmpty(cookies)) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals("listShowStyle")) {
					mv.addObject("listShowStyle", cookie.getValue());
				}
				if (cookie.getName().equals("introStyle")) {
					mv.addObject("introStyle", cookie.getValue());
				}
				if (cookie.getName().equals("compareCartStyle")) {
					mv.addObject("compareCartStyle", cookie.getValue());
				}
			}
		}
		// ------end:把页面list的显示风格设置到attribute中------
		
		// ------start:获得对比车上的商品------
		List<Product> compareProductList = prodMainManager.getCompareProductsFromRequest(request);
		mv.addObject("compareProductList",compareProductList);
		// ------end:  获得对比车上的商品------
		
		return mv;
	}

}
